package next.Functionality;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

public class SmsFunctionality extends MenuFunctionality {

	//--Variables--
	public static enum TypeSMS {
		PROGRAMATICALLY, CUSTOM
	};

	protected IntentFilter intentFilter;
	
	/**The broadcast receiver is attending possible signals from SMS*/
	protected final BroadcastReceiver intentReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			//--What to do with the answer of sended message--
			Toast.makeText(getApplicationContext(), intent.getExtras().getString("sms"),Toast.LENGTH_SHORT).show();
		}
	};

	/** Called before SMS is created, to set SMS features. */
	public void CreateSMS(TypeSMS type, String[] address, String message, String Who) {
		
		//--Default values
		if (type==null || address==null || message==null)
		{
			Log.d("Error Creating SMS", "Null type, address or message");
		}
		//--Defines where is coming the SMS
		if (Who==null)
			Who=new String("");
		
		String AddressFormat = new String("");
		for (int j = 0; j < address.length; j++) {
			AddressFormat += (address[j] + ";");
		}
				
					
		if (type == TypeSMS.PROGRAMATICALLY) {
			// --1 Programatically--
			for (int i = 0; i < address.length; i++) {
				sendSMS(address[i], message);
			}
			
		} else if (type == TypeSMS.CUSTOM) {
			// --2 Built-in Application--
			Intent i = new Intent(android.content.Intent.ACTION_VIEW);
			i.putExtra("address", AddressFormat);
			i.putExtra("sms_body", message);
			i.setType("vnd.android-dir/mms-sms");
			startActivity(i);
		}
		
		

	}

	
	/**Sends an SMS message to another device with feedback.*/
	private void sendSMS(String phoneNumber, String message) {
		String SENT = "SMS_SENT";
		String DELIVERED = "SMS_DELIVERED";
		PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(
				SENT), 0);
		PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
				new Intent(DELIVERED), 0);
		//---when the SMS has been sent---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS sent",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
					Toast.makeText(getBaseContext(), "Generic failure",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NO_SERVICE:
					Toast.makeText(getBaseContext(), "No service",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_NULL_PDU:
					Toast.makeText(getBaseContext(), "Null PDU",
							Toast.LENGTH_SHORT).show();
					break;
				case SmsManager.RESULT_ERROR_RADIO_OFF:
					Toast.makeText(getBaseContext(), "Radio off",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(SENT));

		//--when the SMS has been delivered---
		registerReceiver(new BroadcastReceiver() {
			@Override
			public void onReceive(Context arg0, Intent arg1) {
				switch (getResultCode()) {
				case Activity.RESULT_OK:
					Toast.makeText(getBaseContext(), "SMS delivered",
							Toast.LENGTH_SHORT).show();
					break;
				case Activity.RESULT_CANCELED:
					Toast.makeText(getBaseContext(), "SMS not delivered",
							Toast.LENGTH_SHORT).show();
					break;
				}
			}
		}, new IntentFilter(DELIVERED));
		SmsManager sms = SmsManager.getDefault();
		sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
	}

	/**Register automatic action for the sended message. Is activated when that message  is answered*/
	@Override
	public void onStart() {
		// ---intent to filter for SMS messages received---
				intentFilter = new IntentFilter();
				intentFilter.addAction("SMS_RECEIVED_ACTION");

		// ---Managing Broadcast Receiver-Registration--
				registerReceiver(intentReceiver, intentFilter);
				
		super.onStart();
		
	}

	/**Managing Broadcast Receiver-Unregistration*/
		@Override
	public void onDestroy() {
		// ---unregister the receiver---
		unregisterReceiver(intentReceiver);
		super.onDestroy();
	}

}
